package com.us.usws.site.ws.resource;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.Meeting;
import com.us.usws.model.domain.channel.Channel;
import com.us.usws.model.domain.video.Video;
import com.us.usws.service.ro.ChannelROService;
import com.us.usws.service.ro.MeetingROService;
import com.us.usws.service.ro.VideoROService;
import com.us.usws.site.ws.entity.WSEntity;
import com.us.usws.site.ws.entity.channel.WSChannelEntityPackGenerator;
import com.us.usws.site.ws.entity.shared.WSSharedEntityPackGenerator;
import com.us.usws.site.ws.entity.shared.WSSharedEntityPackGenerator.PageUrlCallback;
import com.us.usws.support.uitls.ServiceFactory;
import com.us.usws.support.uitls.SimpleDataPageUtil;

/**
 * User webservice
 */
@Path("/channels")
public class ChannelResource {

	//private static Log log = LogFactory.getFactory().getInstance(ChannelResource.class);

	private ChannelROService cService = ServiceFactory.getInstance().getService(ChannelROService.class);
	private VideoROService vService = ServiceFactory.getInstance().getService(VideoROService.class);
	private MeetingROService mService = ServiceFactory.getInstance().getService(MeetingROService.class);

	@GET
	@Path("/{channelUid}/sub_channels.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getSubChannels(@PathParam("channelUid") String channelUid, @QueryParam("focus_channel_id") String focusChannelUid) {

		List<Channel> results = cService.getSubChannels(channelUid);

		WSEntity ret = WSChannelEntityPackGenerator.getInstance().getSubChannelsPack(results, focusChannelUid);

		return ret;
	}

	@GET
	@Path("/p_student_channels.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getStudentChannels() {
		List<Channel> channels = cService.getTopChannelsByTargetRole(SiteConstants.CHANNEL_TARGET_ROLE_STUDENT);

		WSEntity ret = WSChannelEntityPackGenerator.getInstance().getStudentChannels(channels);
		return ret;
	}

	@GET
	@Path("/{channelUid}/movies.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getChannelVideos(@PathParam("channelUid") String channelUid, @QueryParam("page_size") int pageSize,
			@QueryParam("page") int pageNo) {

		if (pageSize < 1) {
			pageSize = SiteConstants.DFT_WS_PAGE_SIZE;
		}
		if (pageNo < 1) {
			pageNo = 1;
		}

		int cnt = vService.getChannelVideoCount(channelUid);
		SimpleDataPageUtil pu = new SimpleDataPageUtil(pageSize, cnt);
		pu.setPageNo(pageNo);
		List<Video> results = vService.getChannelVideos(channelUid, pu.getStart(), pageSize);

		WSEntity ret = WSChannelEntityPackGenerator.getInstance().getChannelVideosPack(channelUid, results, pu);

		return ret;
	}

	@GET
	@Path("/{channelUid}/meetings.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getChannelMeetings(@PathParam("channelUid") final String channelUid, @QueryParam("page_size") int pageSize,
			@QueryParam("page") int pageNo) {

		if (pageNo < 1) {
			pageNo = 1;
		}
		if (pageSize < 1) {
			pageSize = SiteConstants.DFT_MEETING_PAGE_SIZE;
		}

		int cnt = mService.getMeetingCountByChannle(channelUid);
		final SimpleDataPageUtil pu = new SimpleDataPageUtil(pageSize, cnt);
		pu.setPageNo(pageNo);
		List<Meeting> results = mService.getMeetingsByChannel(channelUid, pu.getStart(), pageSize);

		WSEntity ret = WSSharedEntityPackGenerator.getInstance().getMeetingsPack(results, pu, new PageUrlCallback() {

			@Override
			public String getPrevPageUrl() {
				return WSResourceURIReplacer.getChannelMeetingsURLWithPageNo(channelUid, pu.getPageNo() - 1, pu.getPageCount());
			}

			@Override
			public String getNextPageUrl() {
				return WSResourceURIReplacer.getChannelMeetingsURLWithPageNo(channelUid, pu.getPageNo() + 1, pu.getPageCount());
			}
		});

		return ret;
	}

	@GET
	@Path("/{channelUid}/two_student_channels.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getLevel2StudentChannels(@PathParam("channelUid") String channelUid) {

		List<Channel> results = cService.getSubChannels(channelUid);

		WSEntity ret = WSChannelEntityPackGenerator.getInstance().getLevel2StudentChannels(results);

		return ret;
	}

	@GET
	@Path("/{channelUid}/stu_meetings.json")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public WSEntity getStudentMeetings(@PathParam("channelUid") final String channelUid, @QueryParam("page_size") int pageSize,
			@QueryParam("page") int pageNo, @QueryParam("mark") int seqNum) {

		if (pageNo < 1) {
			pageNo = 1;
		}
		if (pageSize < 1) {
			pageSize = SiteConstants.DFT_MEETING_PAGE_SIZE;
		}

		int cnt = mService.getMeetingCountByChannelAndSeqNum(channelUid, seqNum);
		final SimpleDataPageUtil pu = new SimpleDataPageUtil(pageSize, cnt);
		pu.setPageNo(pageNo);
		List<Meeting> results = mService.getMeetingsByChannelAndSeqNum(channelUid, seqNum, pu.getStart(), pageSize);

		WSEntity ret = WSSharedEntityPackGenerator.getInstance().getMeetingsPack(results, pu, new PageUrlCallback() {

			@Override
			public String getPrevPageUrl() {
				return WSResourceURIReplacer.getChannelMeetingsURLWithPageNo(channelUid, pu.getPageNo() - 1, pu.getPageCount());
			}

			@Override
			public String getNextPageUrl() {
				return WSResourceURIReplacer.getChannelMeetingsURLWithPageNo(channelUid, pu.getPageNo() + 1, pu.getPageCount());
			}
		});

		return ret;
	}

}
